class ComplaintsController < ApplicationController
  # GET /complaints
  # GET /complaints.xml
  def index
    if !params[:company_id].nil?
      @item=Company.find(params[:company_id])
      @all_items=Company.all
      @complaints = @item.complaints
      @context="company"
    elsif !params[:tag_id].nil?
      @item=Tag.find(params[:tag_id])
      @all_items=Tag.all
      @complaints = @item.complaints
      @context="tag"
    elsif !params[:category_id].nil?
      @item=Category.find(params[:category_id])
      @all_items=Category.all
      @complaints = @item.complaints
      @context="category"
    else 
      @item=nil
      @complaints = Complaint.find(:all,:order=>"created_at DESC")
      @context="all"
    end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @complaints }
    end
  end

  def show
    @complaint = Complaint.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @complaint }
    end
  end

  def new
    @complaint = Complaint.new
    @all_categories=Category.all.collect {|c| [c.title,c.id]}
    @complaint.urls.build
    @complaint.images.build
    @complaint.tags.build
    @complaint.companies.build
    #@complaint.build_user
    @popular_tags=Tag.all.map {|t| t.title}
  end

  # GET /complaints/1/edit
  def edit
    @complaint = Complaint.find(params[:id])
    @all_categories=Category.all.collect {|c| [c.title,c.id]}
  end

  # POST /complaints
  # POST /complaints.xml
  def create
    @complaint = Complaint.new(params[:complaint])
    
    respond_to do |format|
      if @complaint.save
        flash[:notice] = 'Complaint was successfully created.'
        format.html { redirect_to :action => 'verification_pending', :id => @complaint.id}
        format.xml  { render :xml => @complaint, :status => :created, :location => @complaint }
      else
        format.html { redirect_to :action => "new" }
        format.xml  { render :xml => @complaint.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /complaints/1
  # PUT /complaints/1.xml
  def update
    @complaint = Complaint.find(params[:id])
    params[:complaint][:existing_url_attributes] ||= {}
    params[:complaint][:existing_images] ||= {}

    respond_to do |format|
      if @complaint.update_attributes(params[:complaint])
        flash[:notice] = 'Complaint was successfully updated.'
        format.html { redirect_to(@complaint) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @complaint.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /complaints/1
  # DELETE /complaints/1.xml
  def destroy
    @complaint = Complaint.find(params[:id])
    @complaint.destroy

    respond_to do |format|
      format.html { redirect_to(complaints_url) }
      format.xml  { head :ok }
    end
  end
  
  def verification_pending
    @complaint=Complaint.find(params[:id])
  end
  
  def verify
    @complaint = Complaint.find params[:hash].split('-')[1].to_i
    @user=User.find params[:hash].split('-')[0].to_i
    @complaint.activated=true
    @complaint.save
  end
  
  def support
    c= Complaint.find params[:complaint_id]
    c.support_count.nil? ? c.support_count=1 : c.support_count += 1
    if c.save
      session[:supported_complaints] ||= []
      session[:supported_complaints] << c.id
      render :json => {:count=>c.support_count}.to_json
    else
      render :json => {:count=>"error"}.to_json
    end
  end

  ## Custom autocomplete for company and title
  def auto_complete_for_company_title
    re = Regexp.new("^#{params[:company][:title]}", "i")
    find_options = { :order => "title ASC" }
    @selected_companies = Company.find(:all, find_options).select{ |comp| comp.title.match re }.collect {|c| [c.id,c.title]}
    render :inline => "<%= content_tag(:ul, @selected_companies.map { |comp| content_tag(:li, h(comp[1]),:id=>comp[0]) }) %>"
  end
  def auto_complete_for_tag_title
    re = Regexp.new("^#{params[:tag][:title]}", "i")
    find_options = { :order => "title ASC" }
    @selected_tags = Tag.find(:all, find_options).select{ |tag| tag.title.match re }.collect {|t| [t.id,t.title]}
    render :inline => "<%= content_tag(:ul, @selected_tags.map { |t| content_tag(:li, h(t[1]),:id=>t[0]) }) %>"
  end
  
  def increment_support_count
    complaint = Complaint.find(params[:id])
    complaint.support_count += 1
    complaint.save
    render :inline => complaint.support_count
  end

  def search
    filters=params[:filters][0]
    company=Company.find_by_title(filters[:company])
    redirect_to company_complaints_path(company)
  end


end
